package com.weike.wk_android.view.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;

import com.trello.rxlifecycle3.components.support.RxFragmentActivity;
import com.weike.wk_android.R;
import com.weike.wk_android.model.Tab;
import com.weike.wk_android.widget.tabhost.MyFragmentTabHost;

import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 主视图
 */
public class MainActivity extends RxFragmentActivity {
    @BindView(android.R.id.tabhost)
    MyFragmentTabHost tabhost;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        init();
    }

    /**
     * 初始化数据信息
     */
    private void init() {
        initTabHost();
        initTab();
        setTabChangedListener();
    }

    /**
     * 设置Tab切换监听
     */
    private void setTabChangedListener() {
        tabhost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                updateTab();
            }
        });
    }

    /**
     * 更新Tab样式
     */
    private void updateTab() {
        TabWidget tabw = tabhost.getTabWidget();
        for(int i = 0; i < tabw.getChildCount(); i++){
            View view = tabw.getChildAt(i);
            ImageView iv = view.findViewById(R.id.home_tab_image);
            if(i == tabhost.getCurrentTab()){
                ((TextView)view.findViewById(R.id.home_tab_text))
                        .setTextColor(getResources().getColor(R.color.main_bottom_menu_focused));
                iv.setImageResource(Tab.getTabsImgPress()[i]);
            } else {
                ((TextView)view.findViewById(R.id.home_tab_text))
                        .setTextColor(getResources().getColor(R.color.main_bottom_menu));
                iv.setImageResource(Tab.getTabsImg()[i]);
            }
        }
    }

    /**
     * 获取TabItem视图
     *
     * @param i
     * @return
     */
    private View getTabItem(int i) {
        View view = getLayoutInflater().inflate(R.layout.activity_main_bottom, null);

        ((TextView)view.findViewById(R.id.home_tab_text)).setText(Tab.getTabsTxt()[i]);
        if (i == 0) {
            ((TextView)view.findViewById(R.id.home_tab_text))
                    .setTextColor(getResources().getColor(R.color.main_bottom_menu_focused));
            ((ImageView)view.findViewById(R.id.home_tab_image))
                    .setImageResource(Tab.getTabsImgPress()[i]);
        } else {
            ((ImageView)view.findViewById(R.id.home_tab_image))
                    .setImageResource(Tab.getTabsImg()[i]);
        }

        return view;
    }

    /**
     * 添加几个TabSpc
     */
    private void initTab() {
        String[] tabs = Tab.getTabsTxt();
        for (int i = 0; i < tabs.length; i++) {
            TabHost.TabSpec tabSpec = tabhost.newTabSpec(tabs[i]);
            tabSpec.setIndicator(getTabItem(i));
            tabhost.addTab(tabSpec, Tab.getFragments()[i], null);
            tabhost.setTag(i);
        }
    }

    /**
     * 初始化选项卡
     */
    private void initTabHost() {
        // 设置选项卡内容页面
        tabhost.setup(this, getSupportFragmentManager(), R.id.main_content);
    }
}
